package com.briup.cms.service.impl;

import com.briup.cms.bean.Comment;
import com.briup.cms.dao.CommentMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.ICommentService;
import com.briup.cms.dao.extend.CommentExtendMapper;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @Description
 * @date 2021/7/17 21:07
 */

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentExtendMapper CommentExtendMapper;

    public PageInfo<Comment> pageQuery(int pageNum, int pageSize, String keywords) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Comment> list = this.CommentExtendMapper.pageQuery(keywords);
        return new PageInfo(list);
    }

    @Transactional
    public void deleteById(int id) throws CustomerException {
        this.commentMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }

    @Transactional
    public void batchDelete(int[] ids) throws CustomerException {
        if (ids != null && ids.length > 0)
            for (int id : ids)
                this.commentMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }

    @Transactional
    public void checkComment(int id, int status) throws CustomerException {
        Comment commentFromDB = this.commentMapper.selectByPrimaryKey(Integer.valueOf(id));
        if (commentFromDB == null)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_COMMENT_NOT_EXIST);
        Comment comment = new Comment();
        comment.setCommentId(Integer.valueOf(id));
        comment.setCommentStatus(Integer.valueOf(status));
        this.commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Transactional
    public void deleteCommentArticleId(Integer articleId) throws CustomerException{
        this.CommentExtendMapper.deleteCommentArticleId(articleId);
    }

    @Transactional
    public void insert(Comment record) throws CustomerException{
        this.commentMapper.insert(record);
    }
}
